import 'package:dio/dio.dart';
import 'package:get/get.dart' as getx;
import 'package:getx/widgets/loading.dart';
import 'package:loader_overlay/loader_overlay.dart';

class LoadingInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    var context = getx.Get.context!;
    context.loaderOverlay.show(widgetBuilder: (_) => Loading());
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    var context = getx.Get.context!;
    if (context.loaderOverlay.visible) {
      context.loaderOverlay.hide();
    }
    super.onResponse(response, handler);
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    var context = getx.Get.context!;
    if (context.loaderOverlay.visible) {
      context.loaderOverlay.hide();
    }
    super.onError(err, handler);
  }
}
